package cn.wolfcode.web.controller;

import cn.wolfcode.domain.Department;
import cn.wolfcode.domain.Employee;
import cn.wolfcode.domain.Role;
import cn.wolfcode.qo.EmployeeQueryObject;
import cn.wolfcode.qo.JSONResult;
import cn.wolfcode.qo.PageResult;
import cn.wolfcode.service.IDepartmentService;
import cn.wolfcode.service.IEmployeeService;
import cn.wolfcode.service.IRoleService;
import cn.wolfcode.util.RequiredPermission;
import cn.wolfcode.util.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/employee")
public class EmployeeController {
    @Autowired
    private IEmployeeService employeeService;
    @Autowired
    private IDepartmentService departmentService;

    @Autowired
    private IRoleService roleService;


    @RequiredPermission(name = "员工列表", expression = "employee:list")
    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") EmployeeQueryObject qo) {
        PageResult<Employee> pageResult = employeeService.query(qo);

        model.addAttribute("pageResult", pageResult);
        List<Department> departments = departmentService.listAll();
        model.addAttribute("departments", departments);
        return "employee/list";
    }


    @RequestMapping("/delete")
    @ResponseBody
    public JSONResult delete(Long id) {
        try {

            if (id != null) {
                employeeService.delete(id);
            }
            return new JSONResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new JSONResult(false, "删除失败");
        }
    }

    @RequestMapping("/input")
    public String input(Long id, Model model) {

        List<Department> departments = departmentService.listAll();
        model.addAttribute("departments", departments);
        List<Role> roles = roleService.listAll();
        model.addAttribute("roles", roles);
        if (id != null) {
            Employee employee = employeeService.get(id);
            model.addAttribute("employee", employee);
        }
        return "employee/input";
    }

    @RequestMapping("/saveOrUpdate")
    public String saveOrUpdate(Employee employee, Long[] ids) {

        if (employee.getId() != null) {
            employeeService.update(employee, ids);

        } else {
            employeeService.save(employee, ids);
        }
        return "redirect:/employee/list.do";
    }


    @RequestMapping("/inputPwd")
    public String inputPwd() {

        return "/employee/updatePwd";
    }

    @RequestMapping("/updatePwd")
    @ResponseBody
    public JSONResult updatePwd(String oldPassword, String newPassword) {
        //获取当前登录用户
        Employee employee = UserContext.getCurrentUser();
        //密码判空
        if (StringUtils.isEmpty(oldPassword) || StringUtils.isEmpty(newPassword)) {
            return new JSONResult(false, "密码不能为空");
        }
        //获取数据库中密码与输入的原密码作对比
        if (employee.getPassword().equals(oldPassword)) {
            //一致,根据员工id修改数据库中对应员工的密码
            employeeService.updatePwd(employee.getId(), newPassword);
            //注销之前登录信息
            HttpSession session = UserContext.getSession();
            session.invalidate();
            return new JSONResult();
        } else {
            return new JSONResult(false, "旧密码错误,修改失败");
        }
    }

//    @RequiredPermission(name = "重置密码", expression = "employee:inputReset")
    @RequestMapping("/inputReset")
    public String resetPwd(Model model, Long id) {
        Employee employee = employeeService.get(id);

        model.addAttribute("employee", employee);
        return "/employee/resetPwd";

    }

    @RequestMapping("/resetPwd")
    @ResponseBody
    public JSONResult resetPwd(Long id, String newPassword) {
        Employee employee = employeeService.get(id);
        if (newPassword != null) {
            employeeService.resetPwd(employee.getId(), newPassword);
            HttpSession session = UserContext.getSession();
            session.invalidate();
            return new JSONResult();
        } else {
            return new JSONResult(false, "密码不能为空");
        }
    }
}
